package com.xiaoc.bulldozer.common.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CommonUtil {

    private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);

    private static final long ONE_DAY = 86400000L;

    private static final long ONE_HOUR = 3600000L;

    private static final long ONE_MINUTE = 60000L;

    public static String relativeTime(Long milliseconds) {

        if (milliseconds == null) {
            return "刚刚";
        }

        long offset = System.currentTimeMillis() - milliseconds;
        String str;
        if (offset < ONE_MINUTE) {
            str = "刚刚";
        } else if (offset < ONE_HOUR) {
            str = toMinutes(offset) + " 分钟前";
        } else if (offset < ONE_DAY) {
            str = toHours(offset) + " 小时前";
        } else {
            str = toDays(offset) + " 天前";
        }

        return str;

    }

    private static long toDays(long milliseconds) {

        return toHours(milliseconds) / 24L;
    }

    private static long toHours(long milliseconds) {

        return toMinutes(milliseconds) / 60L;
    }

    private static long toMinutes(long milliseconds) {

        return toSeconds(milliseconds) / 60L;
    }

    private static long toSeconds(long milliseconds) {

        return milliseconds / 1000L;
    }

    public static String markPhoneNumber(String phoneNumber) {

        if (StringUtils.isEmpty(phoneNumber)) {
            return null;
        }

        try {
            return phoneNumber.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        } catch (Throwable t) {
            logger.error("regular replace failed", t);
        }

        return null;
    }

    public static List<String> getOptionAppointmentTimeList() {
        List<String> optionAppointmentDateList = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();

        int add = 0;
        for (int i = 0; i < 6; i++) {
            if (i != 0) {
                add = 1;
            }
            cal.add(Calendar.MONTH, add);
//            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            optionAppointmentDateList.add(month + "月");
        }
        return optionAppointmentDateList;
    }


    public static String simpleFormatString(String str) {

        str = StringUtils.trimToNull(str);

        str = StringUtils.trim(str);

        str = StringUtils.removeAll(str, " ");

        return str;
    }

    /**
     * 获取小时
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static void main(String[] args) {
        System.out.println(getHour(new Date()));
    }
}
